home *** CD-ROM | disk | FTP | other *** search
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
-
- public class ChessLogic implements ServletResponseReceiver {
- private boolean responsereceived = false;
- private String response = "";
- private ChessCanvas chesscanvas;
- private ChessGUI chessgui;
- private CommandsForm commandsform;
- private ChessGame chessgame = null;
- private Style12Parser currentboard = null;
- private int click = 0;
- private String movestring = "";
- private int selectfield1 = 0;
- private int selectfield2 = 0;
-
- public ChessLogic(ChessCanvas var1) {
- this.chesscanvas = var1;
- this.chessgui = new ChessGUI(var1);
- this.commandsform = new CommandsForm(this, var1);
- this.drawEmptyBoard();
- this.registerCommands();
- }
-
- public void alert(String var1) {
- MessageForm var2 = new MessageForm("Alert", var1, this.commandsform);
- var2.makeCurrent(CU.getDisplay());
- }
-
- private void drawCurrentBoard() {
- if (this.currentboard == null) {
- this.drawEmptyBoard();
- } else {
- Graphics var4 = this.chessgui.getChessGraphics();
-
- for(int var5 = 8; var5 > 0; --var5) {
- for(int var6 = 1; var6 < 9; ++var6) {
- int var3 = var6 * 10 + var5;
- int var1 = this.currentboard.getPiecePos(var3);
- int var2 = this.currentboard.getColorPos(var3);
- this.chessgui.drawPiece(var4, var1, var2, var3);
- }
- }
-
- if (this.selectfield1 != 0) {
- this.chessgui.selectField(var4, this.selectfield1, 0, 0, 0, false);
- }
-
- if (this.selectfield2 != 0) {
- this.chessgui.selectField(var4, this.selectfield2, 255, 255, 255, false);
- }
-
- }
- }
-
- private void drawEmptyBoard() {
- Graphics var2 = this.chessgui.getChessGraphics();
-
- for(int var3 = 8; var3 > 0; --var3) {
- for(int var4 = 1; var4 < 9; ++var4) {
- int var1 = var4 * 10 + var3;
- this.chessgui.drawPiece(var2, 0, 0, var1);
- }
- }
-
- }
-
- private void drawInformation(Graphics var1) {
- String var2 = "No info";
- if (this.currentboard != null) {
- if (this.isYourMove() && this.responsereceived && !this.movestring.equals("")) {
- var2 = "Your move? " + this.movestring;
- }
-
- if (this.isYourMove() && !this.responsereceived) {
- var2 = "Sending move...";
- }
-
- if (!this.isYourMove()) {
- var2 = "Awaiting opponent move...";
- }
-
- if (this.isYourMove() && this.responsereceived && this.movestring.equals("")) {
- var2 = "Op. moved: " + this.currentboard.getPreviousMoveVerbose() + " your? ";
- }
- } else {
- var2 = "No game active";
- }
-
- this.chessgui.drawInformation(var1, 1, var2);
- }
-
- private void error(String var1) {
- MessageForm var2 = new MessageForm("Error", var1, this.chesscanvas);
- var2.makeCurrent(CU.getDisplay());
- }
-
- private void executeCommand(int var1) {
- if (this.chessgame == null && var1 == 2) {
- this.error("Please logon first");
- } else {
- switch (var1) {
- case 1:
- this.commandsform.makeCurrent(CU.getDisplay());
- break;
- case 2:
- this.move();
- }
-
- }
- }
-
- public ChessGame getChessGame() {
- return this.chessgame;
- }
-
- public CommandsForm getCommandsForm() {
- return this.commandsform;
- }
-
- public void handleBoardClick(int var1, int var2) {
- int var3 = this.chessgui.getFieldCode(var1, var2);
- switch (this.click) {
- case 0:
- this.movestring = CU.int2alg(var3);
- this.selectfield1 = var3;
- this.selectfield2 = 0;
- ++this.click;
- break;
- case 1:
- this.movestring = this.movestring + "-" + CU.int2alg(var3);
- ++this.click;
- this.selectfield2 = var3;
- break;
- case 2:
- this.movestring = CU.int2alg(var3);
- this.click = 1;
- this.selectfield1 = var3;
- this.selectfield2 = 0;
- }
-
- }
-
- public void handleClick(int var1, int var2) {
- if (this.chessgui.inChessBoard(var1, var2)) {
- this.handleBoardClick(var1, var2);
- } else {
- this.handleCommandClick(var1, var2);
- }
-
- this.chesscanvas.repaint();
- this.chesscanvas.serviceRepaints();
- }
-
- public void handleCommandClick(int var1, int var2) {
- if (this.chessgui.getCommand(var1, var2) != 0) {
- this.executeCommand(this.chessgui.getCommand(var1, var2));
- }
-
- }
-
- private boolean isYourMove() {
- if (this.currentboard == null) {
- return false;
- } else if (this.currentboard.getBoardOrientation() == 1 && this.currentboard.getColorToMove() == 0) {
- return true;
- } else {
- return this.currentboard.getBoardOrientation() == 0 && this.currentboard.getColorToMove() == 1;
- }
- }
-
- private void move() {
- this.responsereceived = false;
- if (this.chessgame.postActive()) {
- this.alert("Unable to send command, still postthread active. \nStatus: " + this.chessgame.getStatus());
- } else {
- this.chessgame.doPost("move " + this.movestring, false);
- this.movestring = "";
- this.click = 0;
- }
-
- }
-
- public void paint(Graphics var1) {
- this.drawCurrentBoard();
- var1.setFont((Font)null);
- Font var2 = var1.getFont();
- this.chessgui.drawCommands(var1);
- this.chessgui.drawChessGraphics(var1);
- Font var3 = Font.getFont(var2.getFace(), 0, 8);
- var1.setFont(var3);
- this.drawInformation(var1);
- }
-
- public void receiveServletResponse(String var1) {
- this.response = var1;
- this.responsereceived = true;
- Style12Parser var2 = new Style12Parser();
- if (var2.parseStyle12(var1)) {
- boolean var3 = true;
- if (this.currentboard != null && this.currentboard.getPreviousMoveVerbose().equals(var2.getPreviousMoveVerbose())) {
- var3 = false;
- }
-
- this.currentboard = var2;
- CU.setBoardOrientation(this.currentboard.getBoardOrientation());
- if (var3) {
- this.drawCurrentBoard();
- }
-
- if (this.chessgame != null && !this.isYourMove() && this.chessgame.isPlaying()) {
- this.responsereceived = false;
- this.selectfield1 = 0;
- this.selectfield2 = 0;
- this.chessgame.doPost("nextstate", false);
- }
- }
-
- this.chesscanvas.repaint();
- this.chesscanvas.serviceRepaints();
- }
-
- private void registerCommands() {
- this.chessgui.registerCommand(1, "MENU");
- this.chessgui.registerCommand(2, "MOVE");
- }
-
- public void reset() {
- this.currentboard = null;
- this.drawEmptyBoard();
- this.movestring = "";
- this.click = 0;
- this.selectfield1 = 0;
- this.selectfield2 = 0;
- }
-
- public void setChessGame(ChessGame var1) {
- this.chessgame = var1;
- }
- }
-